#include "Reader.h"
#include "MySQLReader.h"
#include "UrlReader.h"

namespace great_config {


	Reader::Reader(const string &config) : _group(DEFAULT_GROUP) {

	}

	Reader::~Reader() {
	
	}

	void Reader::setGroup(const string &group) {
		_group = group;
	}

	Reader * Reader::factory(const string &file) {
		Reader *reader = NULL;

		Config config(file.empty() ? DEFAULT_CONFIG_FILE : file);

		string source = config.get("source");
		string group = config.get("group");

		if(source == "mysql") {
			reader = (Reader*) new MySQLReader(config.get("source_string"));
		} else if(source == "url") {
			reader = (Reader*) new UrlReader(config.get("source_string"));
		}

		if(!group.empty()) {
			reader->setGroup(group);
		}

		return reader;
	}
}
